** Preamble **
 
clear all
set more off

local path_input  "C:/Users/$user/Dropbox/$project/master/output/data"
local path_output "C:/Users/$user/Dropbox/$project/master/output/tables"

* Note: local directory is the output folder 
cd "`path_output'"

** Loading data **

use 		 `path_input'/before.dta, clear
append using `path_input'/middle.dta
append using `path_input'/after.dta


drop if  metrop3==3 & wave==3
drop if  metrop2==3 & wave==2
drop if  metrop1==3 & wave==1

gen     MA=0
replace MA=1 if estad==16

gen T=0
replace T=1 if MA==0 & wave==3

decode estado, gen(uf)

merge m:1 uf using `path_input'/controls_RM

** Lula **
*W2_q2=Em quem vota no 2o turno
gen vota_lula_2oturno    = (W2_q2==1 | W3_q1==1 | W1_q2==1)

*W2_q3=Se o segundo turno fosse hoje
gen vota_lula_2oturno_v2 = (W2_q3==1 | W3_q2==1 | W1_q3==1)

*Ambos
gen vota_lula_2oturno_v3=vota_lula_2oturno*vota_lula_2oturno_v2

gen weird=0
replace weird=1 if W2_q2==0 | W3_q1==0 | W1_q2==0
replace weird=2 if W2_q2==3 | W3_q1==3 | W1_q2==3
replace weird=3 if W2_q2==4 | W3_q1==4 | W1_q2==4

gen ns=0
replace ns=1 if W2_q2==6 | W3_q1==6 | W1_q2==6


** Collor **
*W2_q2=Em quem vota no 2o turno
gen vota_collor_2oturno   = (W2_q2==2 | W3_q1==2 | W1_q2==2)

*W2_q3=Se o segundo turno fosse hoje
gen vota_collor_2oturno_v2= (W2_q3==2 | W3_q2==2 | W1_q3==2)

*Ambos
gen vota_collor_2oturno_v3=vota_collor_2oturno*vota_collor_2oturno_v2


*Turnout
gen     turnout    = (W2_q15==1 | W2_q15==2)
replace turnout=1 if (W1_q14==1 | W1_q14==2)
replace turnout=1 if (W3_q11==1 | W3_q11==2)

gen votou_1o_turno_LULA    = W2_q1==2
gen votou_1o_turno_BRIZOLA = W2_q1==3
gen votou_1o_turno_COVAS   = W2_q1==4
gen votou_1o_turno_MALUF   = W2_q1==5
gen votou_1o_turno_AFIF    = W2_q1==6
gen votou_1o_turno_ULISSES = W2_q1==7    
gen votou_1o_turno_FREIRE  = W2_q1==8 
gen votou_1o_turno_CHAVES  = W2_q1==9 
gen votou_1o_turno_CAIADO  = W2_q1==10 
gen votou_1o_turno_OTHER   = W2_q1==11 
   
*Assistiu o debate
gen watch3=0
replace watch3=1 if W3_q3<3 & wave==3
gen watch2=0
replace watch2=1 if W2_q11>0 & W2_q11<3 & wave==2 
gen watch=0
replace watch=1  if watch2==1
replace watch=1  if watch3==1
replace watch=.  if W2_q11==0
replace watch=.  if wave==1
drop watch2 watch3

cap drop Lulawon*
gen Lulawon3=0
replace Lulawon3=1 if (W3_q4==1)  & wave==3
gen Lulawon2=0
replace Lulawon2=1 if (W2_q12==1) & wave==2 
gen Lulawon=0
replace Lulawon=1  if Lulawon2==1
replace Lulawon=1  if Lulawon3==1
*replace Lulawon=. if watch==0
replace Lulawon=.  if wave==1
*replace Lulawon=. if W3_q4==0
drop Lulawon2 Lulawon3

cap drop Collorwon*
gen Collorwon3=0
replace Collorwon3=1 if (W3_q4==2)  & wave==3
gen Collorwon2=0
replace Collorwon2=1 if (W2_q12==2) & wave==2 
gen Collorwon=0
replace Collorwon=1  if Collorwon2==1
replace Collorwon=1  if Collorwon3==1
replace Collorwon=.  if wave==1
drop Collorwon2 Collorwon3

gen     titulo=0
replace titulo=1 if W1_q14==1 & wave==1
replace titulo=1 if W2_q15==1 & wave==2
replace titulo=1 if W3_q11==1 & wave==3

gen WW1_SEXO   = SEXO*(wave==1)
gen WW1_escola = escola*(wave==1)
gen WW1_IDADE  = IDADE*(wave==1)
gen WW2_SEXO   = SEXO*(wave==2)
gen WW2_escola = escola*(wave==2)
gen WW2_IDADE  = IDADE*(wave==2)
gen WW3_SEXO   = SEXO*(wave==3)
gen WW3_escola = escola*(wave==3)
gen WW3_IDADE  = IDADE*(wave==3)

drop if titulo==0

cap drop T1
foreach var in years_sch1991_rm renda1991_rm sh_tv1991_rm sh_agua1991_rm sh_elect1991_rm pop1991_rm{
gen T1`var'=`var'*(wave==3)
}
qui eststo: xi: reghdfe vota_lula_2oturno_v2 T T1*, vce(cluster estado) a(estado IDADE SEXO escola wave)
gen sample=e(sample)

keep if sample==1
estimates clear

												** Table 11 **
																								
/*
The table reports regression estimates of the effect of Globo's edited coverage on survey data outcomes. Column (1) reports estimates on the probability the individual answers Lula as the debate winner. Column (2) reports estimates on the probability the individual reports intention to vote on Lula, as in the previous table. Column (3) uses the same outcome as in the previous column but controlling for whether the individual reports Lula won the debate. All specifications include metropolitan areas and time fixed effects. Heteroskedasticity-adjusted standard errors clustered at the municipality level are reported below in brackets. Significa different from zero at $99\% (***)$, $95\% (**)$ and $90\%(*)$ confidence level.
*/

local legend "The table reports regression estimates of the effect of Globo's edited coverage on survey data outcomes. Column (1) reports estimates on the probability the individual answers Lula as the debate winner. Column (2) reports estimates on the probability the individual reports intention to vote on Lula, as in the previous table. Column (3) uses the same outcome as in the previous column but controlling for whether the individual reports Lula won the debate. All specifications include metropolitan areas and time fixed effects. Heteroskedasticity-adjusted standard errors clustered at the municipality level are reported below in brackets. Significa different from zero at $99\% (***)$, $95\% (**)$ and $90\%(*)$ confidence level."


eststo: xi: reghdfe Lulawon 			 T 		   [aw=peso] if watch==1, vce(cluster estado) a(estado IDADE SEXO escola wave)
estadd local DemControls "Yes"

eststo: xi: reghdfe vota_lula_2oturno_v2 T 		   [aw=peso] if watch==1, vce(cluster estado) a(estado IDADE SEXO escola wave)
estadd local DemControls "Yes"

eststo: xi: reghdfe vota_lula_2oturno_v2 T Lulawon [aw=peso] if watch==1, vce(cluster estado) a(estado IDADE SEXO escola wave)
estadd local DemControls "Yes"

label var T 	  "Globo"
label var Lulawon "Lula won"


esttab est1 est2 est3 using effects_survey_winner.tex, ///
keep(T Lulawon) order(T Lulawon) ///
stat(N r2 DemControls, fmt(0 2 0) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{S}{@}" "\multicolumn{1}{c}{@}") ///
label("Observations" "R-squared" "Individual demographics" )) ///
mgroups("Lula won debate" "Lula's vote-share" , pattern(1 1 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cline{@span})) ///
star(* 0.10 ** 0.05 *** 0.01) ///
nomtitles  ///
r2 b(3) replace  se(3) brac compress nonotes label  nogaps staraux ///
nonotes addnotes("\begin{minipage}{.75\linewidth} \footnotesize \smallskip \textbf{Note:} `legend' \end{minipage}" )
